package com.jukte.recipe;

import java.util.ArrayList;
import java.util.List;

import com.ewhat.recipes.R;
import com.jukte.recipe.adapters.RecipeListAdapter;
import com.jukte.recipe.db.DataBaseManager;
import com.jukte.recipe.models.Recipe;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class FavRecipeListActivity extends BaseActivity {

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		fetchDataFromDb();
	}

	private Context con;
	private ListView recipeListView;
	private List<Recipe> recipes;
	private RecipeListAdapter adapter;
	private ProgressDialog m_ProgressDialog;
	private WebView adWebView;
	private View layoutWebView;
	private String webLink = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recipe_list);
		
		init();
		setUpList();
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setLayerTypeToWebview() {
		// TODO Auto-generated method stub
		adWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
	}
	
	@Override
	public void init() {
		super.init();
		tv_title.setText("Favorite Recipes");
		
		layoutWebView = findViewById(R.id.layoutWebView);
		adWebView = (WebView)findViewById(R.id.adWebView);
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            // only for gingerbread and newer versions
			setLayerTypeToWebview();
        }
		adWebView.setBackgroundColor(0x00000000);
		
		
		layoutWebView.setVisibility(View.VISIBLE);
		adWebView.loadUrl("file:///android_res/raw/adweb3.html");
		webLink = "http://tgpcis5.felicitygf.hop.clickbank.net";
		
		con=FavRecipeListActivity.this;
		recipeListView=(ListView)findViewById(R.id.recipe_listview);
		recipes=new ArrayList<Recipe>();
	}

	public void onAdWebClick(View v){
		if(webLink.length()==0)return;
		Uri uri = Uri.parse(webLink);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}
	
	private void fetchDataFromDb() {
		Thread thread = new Thread(null, dbFetch, "MagentoBackground");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(this, "",
				"Fetching data...", true);
		m_ProgressDialog.setCancelable(true);
	}

	Runnable dbFetch=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			getRecipeListFromDb();
			runOnUiThread(returnRecipeData);
		}
	};
	Runnable returnRecipeData=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			m_ProgressDialog.dismiss();
			adapter.notifyDataSetChanged();
			
		}
	};
	private void setUpList()
	{
				//recipes=new ArrayList<Recipe>();
				adapter=new RecipeListAdapter(FavRecipeListActivity.this,R.layout.row_recipelist, recipes);
				recipeListView.setAdapter(adapter);
				recipeListView.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						
						Intent intent=new Intent(con, RecipeDetailsActivity.class);
						intent.putExtra("RECIPE_ID", recipes.get(position).getId());
						startActivity(intent);
						
					}
				});
	}
	
	private void getRecipeListFromDb() {
		// TODO Auto-generated method stub
		recipes.clear();
		try{
			recipes.addAll(DataBaseManager.getInstance().getAllFav());
			DataBaseManager.getInstance().closeDb();
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		//Log.e("aise", sites.size()+"");
	}


}
